package com.geezdata.cps.sdk3rd.payment;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.sdk3rd.payment.enums.PayMethodEnum;
import com.geezdata.cps.sdk3rd.payment.enums.PayWayEnum;
import com.geezdata.cps.sdk3rd.payment.pay.BasePay;
import com.geezdata.cps.sdk3rd.payment.pay.alipay.AliPagePay;
import com.geezdata.cps.sdk3rd.payment.request.CloseRequest;
import com.geezdata.cps.sdk3rd.payment.request.PayRequest;
import com.geezdata.cps.sdk3rd.payment.request.QueryRequest;
import com.geezdata.cps.sdk3rd.payment.request.RefundQueryRequest;
import com.geezdata.cps.sdk3rd.payment.request.RefundRequest;
import com.geezdata.cps.sdk3rd.payment.request.VerifyRequest;

public class DefaultPaymentClient implements PaymentClient {
	private static final Logger logger = LoggerFactory.getLogger(DefaultPaymentClient.class);
	private BasePay pay;
	
	public DefaultPaymentClient(String payMethod, String payWay) {
		super();
		this.pay = createPay(payMethod, payWay);
    	if (this.pay == null) {
    		logger.error("支付方式无效");
    		throw new MyException(ErrorCode.HTTP_EXCEPTION.getCode(), "支付方式无效");
    	}
	}
	
    @Override
	public String pay(PayRequest request) {
        return pay.pay(request);
    }

	@Override
	public String close(CloseRequest request) {
        return pay.close(request);
	}

	@Override
	public String refund(RefundRequest request) {
        return pay.refund(request);
	}

	@Override
	public String query(QueryRequest request) {
		return pay.query(request);
	}

	@Override
	public String refundQuery(RefundQueryRequest request) {
		return pay.refundQuery(request);
	} 
	
	@Override
	public boolean verify(VerifyRequest request) {
		return pay.verify(request);
	}
	
	/*
	 * 	@param payMethod	支付方式(1:支付宝 2:微信 3:银联)
	 * 	@param payWay		支付方式(1:PC,平板 2:手机H5 3:二维码 4:app客户端)
	 * 	@param payWay		支付类型(1:PAY 2:REFUND)
	 */
	
	private BasePay createPay(String payMethod, String payWay) {
		if (StringUtils.isEmpty(payMethod) || StringUtils.isEmpty(payWay)) {
			return null;
		}
		
		if(payMethod.equals(PayMethodEnum.ALIPAY.getName())) {
			if (payWay.equals(PayWayEnum.PAGE.getName())) {
				return new AliPagePay();
			}
		}
		
		return null;
	}
}
